summaryrefslogtreecommitdiffstats
path: root/src/core/hle/service/glue/time/manager.cpp
blob: 059ac3fc97f6e97d949c513117a6b0d6466095d1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include <chrono>

#include "core/core.h"
#include "core/core_timing.h"

#include "common/settings.h"
#include "common/time_zone.h"
#include "core/file_sys/vfs/vfs.h"
#include "core/hle/kernel/svc.h"
#include "core/hle/service/glue/time/manager.h"
#include "core/hle/service/glue/time/time_zone_binary.h"
#include "core/hle/service/psc/time/service_manager.h"
#include "core/hle/service/psc/time/static.h"
#include "core/hle/service/psc/time/system_clock.h"
#include "core/hle/service/psc/time/time_zone_service.h"
#include "core/hle/service/set/system_settings_server.h"
#include "core/hle/service/sm/sm.h"

namespace Service::Glue::Time {
namespace {
s64 CalendarTimeToEpoch(Service::PSC::Time::CalendarTime calendar) {
    constexpr auto is_leap = [](s32 year) -> bool {
        return (((year) % 4) == 0 && (((year) % 100) != 0 || ((year) % 400) == 0));
    };
    constexpr std::array<s32, 12> MonthStartDayOfYear{
        0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334,
    };

    s16 month_s16{calendar.month};
    s8 month{static_cast<s8>(((month_s16 * 43) & ~std::numeric_limits<s16>::max()) +
                             ((month_s16 * 43) >> 9))};
    s8 month_index{static_cast<s8>(calendar.month - 12 * month)};
    if (month_index == 0) {
        month_index = 12;
    }
    s32 year{(month + calendar.year) - !month_index};
    s32 v8{year >= 0 ? year : year + 3};

    s64 days_since_epoch = calendar.day + MonthStartDayOfYear[month_index - 1];
    days_since_epoch += (year * 365) + (v8 / 4) - (year / 100) + (year / 400) - 365;

    if (month_index <= 2 && is_leap(year)) {
        days_since_epoch--;
    }
    auto epoch_s{((24ll * days_since_epoch + calendar.hour) * 60ll + calendar.minute) * 60ll +
                 calendar.second};
    return epoch_s - 62135683200ll;
}

s64 GetEpochTimeFromInitialYear(std::shared_ptr<Service::Set::ISystemSettingsServer>& set_sys) {
    Service::PSC::Time::CalendarTime calendar{
        .year = 2000,
        .month = 1,
        .day = 1,
        .hour = 0,
        .minute = 0,
        .second = 0,
    };
    set_sys->GetSettingsItemValueImpl<s16>(calendar.year, "time",
                                           "standard_user_clock_initial_year");
    return CalendarTimeToEpoch(calendar);
}

Service::PSC::Time::LocationName GetTimeZoneString(Service::PSC::Time::LocationName& in_name) {
    auto configured_zone = Settings::GetTimeZoneString(Settings::values.time_zone_index.GetValue());

    Service::PSC::Time::LocationName configured_name{};
    std::memcpy(configured_name.data(), configured_zone.data(),
                std::min(configured_name.size(), configured_zone.size()));

    if (!IsTimeZoneBinaryValid(configured_name)) {
        configured_zone = Common::TimeZone::FindSystemTimeZone();
        configured_name = {};
        std::memcpy(configured_name.data(), configured_zone.data(),
                    std::min(configured_name.size(), configured_zone.size()));
    }

    ASSERT_MSG(IsTimeZoneBinaryValid(configured_name), "Invalid time zone {}!",
               configured_name.data());

    return configured_name;
}

} // namespace

TimeManager::TimeManager(Core::System& system)
    : m_steady_clock_resource{system}, m_worker{system, m_steady_clock_resource,
                                                m_file_timestamp_worker} {
    m_time_m =
        system.ServiceManager().GetService<Service::PSC::Time::ServiceManager>("time:m", true);

    auto res = m_time_m->GetStaticServiceAsServiceManager(&m_time_sm);
    ASSERT(res == ResultSuccess);

    m_set_sys =
        system.ServiceManager().GetService<Service::Set::ISystemSettingsServer>("set:sys", true);

    res = MountTimeZoneBinary(system);
    ASSERT(res == ResultSuccess);

    m_worker.Initialize(m_time_sm, m_set_sys);

    res = m_time_sm->GetStandardUserSystemClock(&m_file_timestamp_worker.m_system_clock);
    ASSERT(res == ResultSuccess);

    res = m_time_sm->GetTimeZoneService(&m_file_timestamp_worker.m_time_zone);
    ASSERT(res == ResultSuccess);

    res = SetupStandardSteadyClockCore();
    ASSERT(res == ResultSuccess);

    Service::PSC::Time::SystemClockContext user_clock_context{};
    res = m_set_sys->GetUserSystemClockContext(&user_clock_context);
    ASSERT(res == ResultSuccess);

    // TODO the local clock should initialise with this epoch time, and be updated somewhere else on
    // first boot to update it, but I haven't been able to find that point (likely via ntc's auto
    // correct as it's defaulted to be enabled). So to get a time that isn't stuck in the past for
    // first boot, grab the current real seconds.
    auto epoch_time{GetEpochTimeFromInitialYear(m_set_sys)};
    if (user_clock_context == Service::PSC::Time::SystemClockContext{}) {
        m_steady_clock_resource.GetRtcTimeInSeconds(epoch_time);
    }

    res = m_time_m->SetupStandardLocalSystemClockCore(user_clock_context, epoch_time);
    ASSERT(res == ResultSuccess);

    Service::PSC::Time::SystemClockContext network_clock_context{};
    res = m_set_sys->GetNetworkSystemClockContext(&network_clock_context);
    ASSERT(res == ResultSuccess);

    s32 network_accuracy_m{};
    m_set_sys->GetSettingsItemValueImpl<s32>(network_accuracy_m, "time",
                                             "standard_network_clock_sufficient_accuracy_minutes");
    auto one_minute_ns{
        std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::minutes(1)).count()};
    s64 network_accuracy_ns{network_accuracy_m * one_minute_ns};

    res = m_time_m->SetupStandardNetworkSystemClockCore(network_clock_context, network_accuracy_ns);
    ASSERT(res == ResultSuccess);

    bool is_automatic_correction_enabled{};
    res = m_set_sys->IsUserSystemClockAutomaticCorrectionEnabled(&is_automatic_correction_enabled);
    ASSERT(res == ResultSuccess);

    Service::PSC::Time::SteadyClockTimePoint automatic_correction_time_point{};
    res = m_set_sys->GetUserSystemClockAutomaticCorrectionUpdatedTime(
        &automatic_correction_time_point);
    ASSERT(res == ResultSuccess);

    res = m_time_m->SetupStandardUserSystemClockCore(is_automatic_correction_enabled,
                                                     automatic_correction_time_point);
    ASSERT(res == ResultSuccess);

    res = m_time_m->SetupEphemeralNetworkSystemClockCore();
    ASSERT(res == ResultSuccess);

    res = SetupTimeZoneServiceCore();
    ASSERT(res == ResultSuccess);

    s64 rtc_time_s{};
    res = m_steady_clock_resource.GetRtcTimeInSeconds(rtc_time_s);
    ASSERT(res == ResultSuccess);

    // TODO system report "launch"
    //      "rtc_reset" = m_steady_clock_resource.m_rtc_reset
    //      "rtc_value" = rtc_time_s

    m_worker.StartThread();

    m_file_timestamp_worker.m_initialized = true;

    s64 system_clock_time{};
    if (m_file_timestamp_worker.m_system_clock->GetCurrentTime(&system_clock_time) ==
        ResultSuccess) {
        Service::PSC::Time::CalendarTime calendar_time{};
        Service::PSC::Time::CalendarAdditionalInfo calendar_additional{};
        if (m_file_timestamp_worker.m_time_zone->ToCalendarTimeWithMyRule(
                &calendar_time, &calendar_additional, system_clock_time) == ResultSuccess) {
            // TODO IFileSystemProxy::SetCurrentPosixTime(system_clock_time,
            // calendar_additional.ut_offset)
        }
    }
}

TimeManager::~TimeManager() {
    ResetTimeZoneBinary();
}

Result TimeManager::SetupStandardSteadyClockCore() {
    Common::UUID external_clock_source_id{};
    auto res = m_set_sys->GetExternalSteadyClockSourceId(&external_clock_source_id);
    ASSERT(res == ResultSuccess);

    s64 external_steady_clock_internal_offset_s{};
    res = m_set_sys->GetExternalSteadyClockInternalOffset(&external_steady_clock_internal_offset_s);
    ASSERT(res == ResultSuccess);

    auto one_second_ns{
        std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::seconds(1)).count()};
    s64 external_steady_clock_internal_offset_ns{external_steady_clock_internal_offset_s *
                                                 one_second_ns};

    s32 standard_steady_clock_test_offset_m{};
    m_set_sys->GetSettingsItemValueImpl<s32>(standard_steady_clock_test_offset_m, "time",
                                             "standard_steady_clock_test_offset_minutes");
    auto one_minute_ns{
        std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::minutes(1)).count()};
    s64 standard_steady_clock_test_offset_ns{standard_steady_clock_test_offset_m * one_minute_ns};

    auto reset_detected = m_steady_clock_resource.GetResetDetected();
    if (reset_detected) {
        external_clock_source_id = {};
    }

    Common::UUID clock_source_id{};
    m_steady_clock_resource.Initialize(&clock_source_id, &external_clock_source_id);

    if (clock_source_id != external_clock_source_id) {
        m_set_sys->SetExternalSteadyClockSourceId(clock_source_id);
    }

    res = m_time_m->SetupStandardSteadyClockCore(
        reset_detected, clock_source_id, m_steady_clock_resource.GetTime(),
        external_steady_clock_internal_offset_ns, standard_steady_clock_test_offset_ns);
    ASSERT(res == ResultSuccess);
    R_SUCCEED();
}

Result TimeManager::SetupTimeZoneServiceCore() {
    Service::PSC::Time::LocationName name{};
    auto res = m_set_sys->GetDeviceTimeZoneLocationName(&name);
    ASSERT(res == ResultSuccess);

    auto configured_zone = GetTimeZoneString(name);

    if (configured_zone != name) {
        m_set_sys->SetDeviceTimeZoneLocationName(configured_zone);
        name = configured_zone;

        std::shared_ptr<Service::PSC::Time::SystemClock> local_clock;
        m_time_sm->GetStandardLocalSystemClock(&local_clock);

        Service::PSC::Time::SystemClockContext context{};
        local_clock->GetSystemClockContext(&context);
        m_set_sys->SetDeviceTimeZoneLocationUpdatedTime(context.steady_time_point);
    }

    Service::PSC::Time::SteadyClockTimePoint time_point{};
    res = m_set_sys->GetDeviceTimeZoneLocationUpdatedTime(&time_point);
    ASSERT(res == ResultSuccess);

    auto location_count = GetTimeZoneCount();
    Service::PSC::Time::RuleVersion rule_version{};
    GetTimeZoneVersion(rule_version);

    std::span<const u8> rule_buffer{};
    size_t rule_size{};
    res = GetTimeZoneRule(rule_buffer, rule_size, name);
    ASSERT(res == ResultSuccess);

    res = m_time_m->SetupTimeZoneServiceCore(name, rule_version, location_count, time_point,
                                             rule_buffer);
    ASSERT(res == ResultSuccess);

    R_SUCCEED();
}

} // namespace Service::Glue::Time